<?php
/////////////////////////////////////////////////////////////////////////////
// 
//
// workflow_system 
//
// workflow_copyright
// 
/////////////////////////////////////////////////////////////////////////////

/**
 * FILE_NAME : news.class.php   FILE_PATH : E:\www\\trunk\classes\application\news.class.php
 * ....信息操作类
 *
 * @copyright workflow_system 
 * @author workflow Develop Team 
 * @package 
 * @subpackage 
 * @version Wed Jul 02 14:09:03 CST 2008
 */
defined('Base_Use_Sign') or die("this is invalid system!please contact <a href='http://www.shopnc.net'>www.shopnc.net</>.");
class NewsClass extends FrameWork {
	/**
	 * 信息列表 
	 * @author workflow Develop Team     
	 * @param  $condition,$obj_page
	 * @return array
	 */
	function listNews($condition,&$obj_page){
		$conditionstr = $this->_conditionSearch($condition);
		$news_array = $GLOBALS['db']->GetList($conditionstr, $obj_page, "news");
		return $news_array;
	}
	
	/**
	 * 构造检索条件
	 * @author workflow Develop Team     
	 * @param  
	 * @return int/bool/object/array
	 */
	function _conditionSearch($condition){
		$condition_str = "";
		//类别ID
		if ($condition['news_c_id'] != ''){
			if (is_array($condition['news_c_id'])){
				$condition_str .= "and (";
				for ($i=0;$i<count($condition['news_c_id']);$i++){
					if (intval($condition['news_c_id'][$i]) > 0){
						$condition_str .= "  news_c_id = '" . intval($condition['news_c_id'][$i]) . "' or ";
					}
				}
				//去掉最后一个or
				$condition_str = substr(trim($condition_str),0,(strlen(trim($condition_str))-2));
				$condition_str .= ")";
			}else {
				$condition_str .=" and news_c_id='" . $condition['news_c_id'] . "'";
			}
		}
		
		//标题
		if ($condition['news_title'] != ''){
			$condition_str .= " and news_title like '%" . $condition['news_title'] . "%'";
		}
		//order by
		if ($condition['order_by'] != ''){
			if ($condition['order_by'] == 'news_time_desc'){
				$condition_str .=" order by news_date desc";
			}
			if ($condition['order_by'] == 'news_sort_asc'){
				$condition_str .=" order by news_sort asc";
			}
			if ($condition['order_by'] == 'news_sort_desc'){
				$condition_str .=" order by news_sort desc";
			}
		}
		//显示条数
		if ($condition['line_num'] != ''){
			$condition_str .= " limit " . $condition['line_num'];
		}
		return $condition_str;
	}
	
	/**
	 * 信息添加 
	 * @author workflow Develop Team     
	 * @param  $value_array
	 * @return bool
	 */
	function addNews($value_array){
		if (is_array($value_array)){
			$valueArray = array();
			foreach ($value_array as $k => $v){
				if ($v != ''){
					$valueArray[$k] = $v;
				}
			}
			$result = $GLOBALS['db']->InsertRow($valueArray,"news","news_id");
			unset($value_array,$valueArray);
			return $result;
		}
		return false;
	}
	
	/**
	 * 取信息
	 * @author workflow Develop Team     
	 * @param  $id
	 * @return array
	 */
	function getNews($id,$type='simple'){
		if (intval($id) > 0){
			if ($type == 'simple'){
				$value_array = $GLOBALS['db']->GetOneRow(intval($id),'news','news_id');
			}else if ($type == 'more'){
				$value_array = $GLOBALS['db']->GetOneJoinArray(array('news','news_class'),"left join",array("news.news_c_id=news_class.news_c_id"),array('*','news_c_name'),"and news.news_id='". intval($id) ."'",array(" news_c_id desc"));
			}
			return $value_array;
		}
		return false;
	}
	
	/**
	 * 修改信息
	 * @author workflow Develop Team     
	 * @param  $value_array
	 * @return bool
	 */
	function updateNews($value_array){
		if (is_array($value_array) && intval($value_array['news_id']) > 0){
			$valueArray = array();
			foreach ($value_array as $k => $v){
				$valueArray[$k] = $v;
			}
			$result = $GLOBALS['db']->UpdateRow(intval($valueArray['news_id']),$valueArray, 'news', 'news_id');
			unset($value_array,$valueArray);
			return $result;
		}
		return false;
	}
	
	/**
	 * 删除信息
	 * @author workflow Develop Team     
	 * @param  $id
	 * @return bool
	 */
	function delNews($id){
		if (intval($id) > 0){
			$result = $GLOBALS['db']->DeleOneRow(intval($id),'news','news_id');
			return $result;
		}
		return false;
	}
	
	/**
	 * 根据类别ID删除信息
	 * @author workflow Develop Team     
	 * @param  $id 类别ID
	 * @return bool
	 */
	function delNewsByClassId($id){
		if (intval($id) > 0){
			$result = $GLOBALS['db']->DeleOneRow(intval($id),'news','news_c_id');
			return $result;
		}
		return false;
	}
	
	/**
	 * 信息类别列表
	 * @author workflow Develop Team     
	 * @param  
	 * @return array
	 */
	function listNewsClass($condition,&$obj_page){
		$conditionstr = $this->_conditionClassSearch($condition);
		$class_array = $GLOBALS['db']->GetList($conditionstr, $obj_page, "news_class");
		//销毁变量
		unset($conditionstr);
		return $class_array;
	}
	
	/**
	 * 构造类别检索条件
	 * @author workflow Develop Team     
	 * @param  
	 * @return int/bool/object/array
	 */
	function _conditionClassSearch($condition){
		$condition_str = "";
		
		//排除的类别ID
		if ($condition['no_id'] != ''){
			//数组形式
			if (is_array($condition['no_id'])){
				for ($i=0;$i<count($condition['no_id']);$i++){
					if (intval($condition['no_id'][$i]) > 0){
						$condition_str .= " and news_c_id != '" . intval($condition['no_id'][$i]) . "'";
					}
				}
			}else {//单个形式
				$condition_str .= " and news_c_id != '" . $condition['no_id'] . "'";
			}
		}
		//类别ID
		if ($condition['id'] != ''){
			//数组形式
			if (is_array($condition['id'])){
				$condition_str .= "and (";
				for ($i=0;$i<count($condition['id']);$i++){
					if (intval($condition['id'][$i]) > 0){
						$condition_str .= "  news_c_id = '" . intval($condition['id'][$i]) . "' or ";
					}
				}
				//去掉最后一个or
				$condition_str = substr(trim($condition_str),0,(strlen(trim($condition_str))-2));
				$condition_str .= ")";
			}else {//单个形式
				$condition_str .= " and news_c_id = '" . $condition['id'] . "'";
			}
		}
		
		return $condition_str;
	}
	
	/**
	 * 新增信息类别
	 * @author workflow Develop Team     
	 * @param  $value_array
	 * @return bool
	 */
	function addNewsClass($value_array){
		if (is_array($value_array)){
			$valueArray = array();
			foreach ($value_array as $k => $v){
				if ($v != ''){
					$valueArray[$k] = $v;
				}
			}
			$result = $GLOBALS['db']->InsertRow($valueArray,"news_class","news_c_id");
			unset($value_array,$valueArray);
			return $result;
		}
		return false;
	}
	
	/**
	 * 取类别信息
	 * @author workflow Develop Team     
	 * @param  $id
	 * @return array
	 */
	function getNewsClass($id){
		if (intval($id) > 0){
			$value_array = $GLOBALS['db']->GetOneRow(intval($id),'news_class','news_c_id');
			return $value_array;
		}
		return false;
	}
	
	/**
	 * 更新类别
	 * @author workflow Develop Team     
	 * @param  $value_array
	 * @return bool
	 */
	function updateNewsClass($value_array){
		if (is_array($value_array) && intval($value_array['news_c_id']) > 0){
			$valueArray = array();
			foreach ($value_array as $k => $v){
				if ($v != ''){
					$valueArray[$k] = $v;
				}
			}
			$result = $GLOBALS['db']->UpdateRow(intval($valueArray['news_c_id']),$valueArray, 'news_class', 'news_c_id');
			unset($value_array,$valueArray);
			return $result;
		}
		return false;
	}
	
	/**
	 * 删除类别
	 * @author workflow Develop Team     
	 * @param  $id
	 * @return bool
	 */
	function delNewsClass($id){
		if (intval($id) > 0){
			$result = $GLOBALS['db']->DeleOneRow(intval($id),'news_class','news_c_id');
			return $result;
		}
		return false;
	}
	
	/**
	 * 取类别最大ID
	 * @author workflow Develop Team     
	 * @param  
	 * @return int
	 */
	function getNewsClassLastId(){
		$class_array = $GLOBALS['db']->GetList('',$page,'news_class','*',0,'news_c_id desc');
		$class_last_id = $class_array[0]['news_c_id'];
		//销毁变量
		unset($class_array);
		return $class_last_id;
	}
	
	/**
	 * 转移类别下的信息到另一个类别中
	 * @author workflow Develop Team     
	 * @param  $move_array需要转移的新闻ID数组,$to_id转入的类别ID
	 * @return bool
	 */
	function moveNewsClass($move_array,$to_id){
		if (intval($to_id) > 0 && !empty($move_array)){
			foreach ($move_array as $k => $v){
				$condition_str[] = "news_id = '" . $v . "'";
			}
			$condition_str = implode(' or ',$condition_str);
			$value_array = array();
			$value_array['news_c_id'] = intval($to_id);
			if (substr(trim($condition_str),0,3) == 'and'){
				$condition_str = substr(trim($condition_str),3);
			}
			$result = $GLOBALS['db']->UpdateRows('news',$value_array,$condition_str);
			unset($condition,$condition_str,$value_array);
			return $result;
		}
		return false;
	}
	
	/**
	 * 信息JS列表 
	 * @author workflow Develop Team     
	 * @param  $condition,$obj_page
	 * @return array
	 */
	function listNewsJs($condition,&$obj_page){
		$conditionstr = $this->_conditionJsSearch($condition);
		$js_array = $GLOBALS['db']->GetList($conditionstr, $obj_page, "news_js");
		return $js_array;
	}
	
	/**
	 * 构造信息JS检索条件
	 * @author workflow Develop Team     
	 * @param  
	 * @return string
	 */
	function _conditionJsSearch($condition){
		$conditionstr = "";
		//排序
		if ($condition['order'] != ''){
			$conditionstr .= $condition['order'];
		}
		return $conditionstr;
	}
	
	/**
	 * JS添加 
	 * @author workflow Develop Team     
	 * @param  $value_array
	 * @return bool
	 */
	function addNewsJs($value_array){
		if (is_array($value_array)){
			$valueArray = array();
			foreach ($value_array as $k => $v){
				if ($v != ''){
					$valueArray[$k] = $v;
				}
			}
			$result = $GLOBALS['db']->InsertRow($valueArray,"news_js","news_js_id");
			unset($value_array,$valueArray);
			return $result;
		}
		return false;
	}
	
	/**
	 * 取JS
	 * @author workflow Develop Team     
	 * @param  $id
	 * @return array
	 */
	function getNewsJs($id){
		if (intval($id) > 0){
			$value_array = $GLOBALS['db']->GetOneRow(intval($id),'news_js','news_js_id');
			return $value_array;
		}
		return false;
	}
	
	/**
	 * 更新js
	 * @author workflow Develop Team     
	 * @param  $value_array
	 * @return bool
	 */
	function updateNewsJs($value_array){
		if (is_array($value_array) && intval($value_array['news_js_id']) > 0){
			$valueArray = array();
			foreach ($value_array as $k => $v){
				if ($v != ''){
					$valueArray[$k] = $v;
				}
			}
			$result = $GLOBALS['db']->UpdateRow(intval($valueArray['news_js_id']),$valueArray, 'news_js', 'news_js_id');
			unset($value_array,$valueArray);
			return $result;
		}
		return false;
	}
	
	/**
	 * 删除js
	 * @author workflow Develop Team     
	 * @param  $id
	 * @return bool
	 */
	function delNewsJs($id){
		if (intval($id) > 0){
			$result = $GLOBALS['db']->DeleOneRow(intval($id),'news_js','news_js_id');
			@unlink('../html/news_javascript/'.md5($id).'.js');
			return $result;
		}
		return false;
	}
	
	/**
	 * 生成JS调用文件
	 * @author workflow Develop Team     
	 * @param  $id
	 * @return bool
	 */
	function make_js_file($id,$site_url,$charset){
		//判断缓存文件夹是否存在，不存在则创建
		if (!is_dir("../html/news_javascript")){
			//如果不能改变文件夹权限,则返回错误
			if (!mkdir("../html/news_javascript",0777)) {
				return false;
			}
		}
		//取JS信息
		$is_array = $this->getNewsJs($id);
		if ($is_array['news_js_id'] == ''){
			return false;
		}
		//分割信息类别,取信息
		$cid_array = @explode("|",trim($is_array['news_c_id'],'|'));
		//取信息
		$conditon_news['news_c_id'] = $cid_array;
		$conditon_news['line_num']  = $is_array['line_num'];
		$conditon_news['order_by']  = $is_array['order_by'];
		$news_list = $this->listNews($conditon_news,$page);
		//整理信息字数
		if (is_array($news_list)){
			//字符串处理方法
			for ($i=0;$i<count($news_list);$i++){
				$news_list[$i]['cut_title'] = Char_class::cut_str($news_list[$i]['news_title'],$is_array['char_num'],0,$charset);
			}
		}
		//生成JS格式的文件内容
		if (is_array($news_list)){
			$line = '';
			for ($i=0;$i<count($news_list);$i++){
				$line .=  "document.write(\"";
				$line .= "<li>";
				$news_list[$i]['news_title'] = str_replace("\"","\\\"",$news_list[$i]['news_title']);
				$news_list[$i]['cut_title'] = str_replace("\"","\\\"",$news_list[$i]['cut_title']);
				$line .= "<a href='";
				if ($news_list[$i]['news_jump_url'] != ''){
					$line .= $news_list[$i]['news_jump_url'];
				}else {
					$line .= $site_url."/"."home/news.php?id=".$news_list[$i]['news_id'];
				}
				$line .= "' target='_blank' title='".$news_list[$i]['news_title']."'>";
				$line .= $news_list[$i]['cut_title'];
				$line .= "</a>";
				$line .= "</li>";
				$line .= "\");";
			}
		}
		//文件名
		$file_name = '../html/news_javascript/'.md5($id).'.js';
		//生成缓存文件
		require_once("makehtml.class.php");
		$result = MakeHtml::installhtmlfile($file_name, $line);
		//销毁变量
		unset($is_array,$cid_array,$conditon_news,$news_list,$line,$file_name);
		return $result;
	}
	
	/**
	 * 取JS最大ID
	 * @author workflow Develop Team     
	 * @param  
	 * @return int
	 */
	function getNewsJsLastId(){
		$js_array = $GLOBALS['db']->GetList('',$page,'news_js','*',0,'news_js_id desc');
		$js_last_id = $js_array[0]['news_js_id'];
		//销毁变量
		unset($js_array);
		return $js_last_id;
	}
}
?>